﻿var regionID;
var schoolName;
var category;
var address ;
var description;

$(function() {
    $("#txt_Region").regions({ dataurl: rootpath + "api/Region.ashx", parmsdata: { type: "getCity", pid: 0} });
    
    $("form").validate({
        rules: { txt_Region: "required", txt_SchoolName: "required", txt_Address: "required" },
        submitHandler: function() {

            $("#btn_submit").attr("disabled", "disabled").after("<span>提交中.....</span>");

            getValue();
            
            addSchool();
        }
    })
    formReady();
});

function addSchool() {
    $.ajax({
        type: "POST",
        url: "../API/School.ashx",
        data: { type: 'addSchool', schoolName: schoolName, regionID: regionID, category: category, address: address, description: description },
        dataType: "html",
        success: function(data) {
            if (data == 1) {
                $.facebox("创建学校成功！");
            }
            else {
                $.facebox("创建学校失败！");
            }

            clearValue();
        }
    }); 
}

function clearValue() {
    $("#txt_SchoolName").val("");
    $("#txt_Address").val("");
    $("#txt_Description").val("");
    $("#btn_submit").attr("disabled", "").next("span").remove();
}

function getValue() {
    regionID = $("#txt_Region").attr("rel");
    schoolName = $.trim($("#txt_SchoolName").val());
    category = $("#sel_Category option:selected").val();
    address = $.trim($("#txt_Address").val());
    description = $.trim($("#txt_Description").val());
}